var API = API || {};

API.x_excForm = {
    ExcursionsForm: function(enableDays, trigger) {


        var parent = this;
        var calendar = "#calendar";         // element to attach calendar
        var check_in_id = "#check_in";      // element to attach date picker
        var limit = 1;                      // date picker today date (0 -  is available, 1 - not available)
        var startDays = enableDays;

        this.Data = {
            StartDate: null
        };

        this.Calendar = {
            OnSelect: function(dateStr, init) {

                var date = parent.Calendar.GetCalendarDate();
                parent.DatePicker.SetDate(check_in_id, date);
                $(trigger).trigger('change'); // Calculation

            },
            BeforeShowDay: function(date) {

                var day = date.getDay();
                var isEnabled = $.inArray(day, startDays) === -1 ? false : true;

                return [isEnabled, 'classFalse'];

            },
            GetCalendarDate: function() {
                return $(calendar).datepicker('getDate');
            },
            SetCalendarDate: function(date) {
                return $(calendar).datepicker('setDate', date);
            },
            Initialize: function() {

                $(calendar).datepicker({
                    minDate: parent.DatePicker.NextDayDate(new Date(), limit),
                    maxDate: new Date(new Date().getFullYear() + 1, 11, 31),
                    numberOfMonths: 3,
                    showStatus: true,
                    inline: true,
                    showOtherMonths: false,
                    beforeShowDay: this.BeforeShowDay,
                    onSelect: parent.Calendar.OnSelect,
                    firstDay: 1 // Start with Monday
                });

            }

        };

        this.DatePicker = {
            OnSelect: function(dateStr) {

                var date = parent.DatePicker.GetDate(check_in_id);
                parent.Calendar.SetCalendarDate(date);
                $(trigger).trigger('change'); // Calculation

            },
            SetDate: function(ID, date) {
                $(ID).datepicker('setDate', date);
            },
            SetMinDate: function(ID, date) {
                $(ID).datepicker('option', 'minDate', date);
            },
            GetDate: function(ID, format) {
                return $(ID).datepicker('getDate');
            },
            NextDayDate: function(date, noDays) {

                myDate = new Date(date);

                var limitDays = noDays;

                if (myDate.getHours() > 12) {
                    limitDays += 1;
                }

                myDate.setDate(myDate.getDate() + limitDays);
                return myDate;
            }
        };

        this.Initialize = function() {

            this.Calendar.Initialize();

            // Initialize Date pickers inputs and attach on change event :: api/form.js
            var xPicker = new API.x_picker.DateManager(check_in_id, limit);
            xPicker.Initialize();

            // Attach on change event on date pickers inputs
            $(check_in_id).datepicker('option', {onSelect: this.DatePicker.OnSelect});
            $(check_in_id).datepicker('option', "beforeShowDay", this.Calendar.BeforeShowDay);
            $(check_in_id).datepicker('option', "maxDate", new Date(new Date().getFullYear() + 1, 11, 31));

            // Selected dates
            this.DatePicker.OnSelect(false);

        };

    }

};